package xyz.xiezc.spider.common;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.URLUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.net.URL;
import java.nio.charset.Charset;

/**
 * @author wb-xzc291800
 * @date 2019/04/12 16:11
 */
@Slf4j
public class XUtil {

    public static String getComplieUrl(String nextReq, Request currentReq) {
        URL url = URLUtil.url(currentReq.getUrl());
        String protocol = url.getProtocol();
        String host = url.getHost();
        if (nextReq.startsWith("//")) {
            nextReq = protocol + ":" + nextReq;
        } else if (nextReq.startsWith("/")) {
            nextReq = protocol + "://" + host + nextReq;
        }
        return nextReq.trim();
    }

    public static void foreach(int count, Runnable runnable) {
        for (int i = 0; i < count; i++) {
          try {
              runnable.run();
          }catch (Exception e){
              log.error("foreach error", e);
          }
        }
    }

    public static Document getDocument(Response response) {
        byte[] bodyByte = response.getBodyByte();
        Charset charset = response.getCharset();
        if (charset == null) {
            charset = CharsetUtil.CHARSET_UTF_8;
        }
        return Jsoup.parse(new String(bodyByte, charset));
    }
}
